package com.qdsg.ysg.doctor.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.ui.widget.FlowLayoutManager;
import com.qdsg.ysg.doctor.ui.widget.SpaceItemDecoration;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.DicResponse;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 医生擅长编辑
 *
 * @time 2020/11/26 11:01
 */

public class DoctorGoodAtEditActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_save)
    TextView tvSave;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    List<DicResponse.Dic> list = new ArrayList<>();
    List<String> listCode = new ArrayList<>();
    boolean isContain = false;

    private FlowAdapter flowAdapter;
    private String goodAtCode;
    private List<String> stringList;


    @Override
    protected void initViews() {
        tvTitle.setText( "擅长" );
        tvSave.setVisibility( View.VISIBLE );
        FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        recyclerView.addItemDecoration( new SpaceItemDecoration( dp2px() ) );
        recyclerView.setLayoutManager( flowLayoutManager );
        flowAdapter = new FlowAdapter( this, list );
        recyclerView.setAdapter( flowAdapter );
    }

    @Override
    protected void initData() {
        goodAtCode = getIntent().getStringExtra( "goodAtCode" );
        if (!UtilString.isEmpty( goodAtCode )) {
            stringList = Arrays.asList( goodAtCode.split( "," ) );
            listCode.addAll( stringList );
            Log.e( "擅长回显---", listCode.toString() );
        }
        getDictLists();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_base_list;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    private void getDictLists() {
        RestProxy.getInstance().getDicList( "", "doc_good_at", new Observer<DicResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(DicResponse dicResponse) {
                try {
                    //这里写可能出现异常的语句
                    list.clear();
                    list.addAll( dicResponse.data );
                    flowAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    // 捕获异常，如果要捕获Json数据异常，写catch(JSONException e)
                    e.printStackTrace();


                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( DoctorGoodAtEditActivity.this, e );
            }

            @Override
            public void onComplete() {
            }
        } );


    }


    @OnClick(R.id.tv_save)
    public void onViewClicked() {
        if (listCode.size() <= 0) {
            ToastUtil.showToastCenter( mContext, "请选择擅长" );
        } else {

            Log.e( "擅长字符串---", UtilString.listToString( listCode ) );
            RestProxy.getInstance().saveUserInfo( 3, UtilString.listToString( listCode ), new Observer<BaseResponse>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(BaseResponse baseResponse) {
                    try {
                        Intent intent = new Intent();
                        intent.putExtra( "editData", UtilString.listToString( listCode ) );
                        setResult( RESULT_OK, intent );
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError( DoctorGoodAtEditActivity.this, e );
                }

                @Override
                public void onComplete() {

                }
            } );
        }
    }

    class FlowAdapter extends RecyclerView.Adapter<FlowAdapter.ViewHolder> {

        Context mContext;
        List<DicResponse.Dic> mList;


        FlowAdapter(Context context, List<DicResponse.Dic> list) {
            this.mContext = context;
            this.mList = list;
        }

        @NotNull
        @Override
        public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            ViewHolder myHolder;
            myHolder = new ViewHolder( LayoutInflater.from( mContext ).inflate( R.layout.flow_item, parent, false ) );
            return myHolder;


        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.flowText.setText( mList.get( position ).name );
            if (stringList.size() > 0) {
                for (int i = 0; i < stringList.size(); i++) {
                    if (stringList.get( i ).equals( mList.get( position ).code )) {
                        holder.flowText.setBackground( ContextCompat.getDrawable( mContext, R.drawable.item_bg_green ) );
                        holder.flowText.setTextColor( ContextCompat.getColor( mContext, R.color.mainGreen ) );
                    }
                }
            }


            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (listCode.size() > 0) {
                        for (int i = 0; i < listCode.size(); i++) {
                            if (listCode.get( i ).equals( mList.get( position ).code )) {
                                isContain = true;
                            }
                        }
                        if (isContain) {
                            //之前选中过
                            listCode.remove( mList.get( position ).code );
                            holder.flowText.setBackground( ContextCompat.getDrawable( mContext, R.drawable.item_bg_grey_login ) );
                            holder.flowText.setTextColor( ContextCompat.getColor( mContext, R.color.commonGrey ) );
                        } else {
                            listCode.add( mList.get( position ).code );
                            holder.flowText.setBackground( ContextCompat.getDrawable( mContext, R.drawable.item_bg_green ) );
                            holder.flowText.setTextColor( ContextCompat.getColor( mContext, R.color.mainGreen ) );
                        }
                        isContain = false;

                        Log.e( "擅长list---", listCode.size() + "" );
                    } else {
                        holder.flowText.setBackground( ContextCompat.getDrawable( mContext, R.drawable.item_bg_green ) );
                        holder.flowText.setTextColor( ContextCompat.getColor( mContext, R.color.mainGreen ) );
                        listCode.add( mList.get( position ).code );
//                        notifyDataSetChanged();
                    }


                }
            } );


        }

        @Override
        public int getItemCount() {
            return list.size();
        }


        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.flow_text)
            TextView flowText;

            public ViewHolder(View itemView) {
                super( itemView );
                ButterKnife.bind( this, itemView );
            }
        }

    }


    private int dp2px() {
        return (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, (float) 10, getResources().getDisplayMetrics() );
    }

}
